<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
    >
      <!-- <el-form-item label="关联藏品ID" prop="collectionId">
        <el-input
          v-model="queryParams.collectionId"
          placeholder="请输入关联藏品ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="影像文件名" prop="imageFilename">
        <el-input
          v-model="queryParams.imageFilename"
          placeholder="请输入影像文件名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="藏品总登记号" prop="generalAccessionNumber">
        <el-input
          v-model="queryParams.generalAccessionNumber"
          placeholder="请输入藏品总登记号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="文件格式" prop="fileFormat">
        <el-input
          v-model="queryParams.fileFormat"
          placeholder="请输入文件格式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="加工日期" prop="processingDate">
        <el-date-picker
          v-model="queryParams.processingDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="加工单位" prop="processingUnit">
        <el-input
          v-model="queryParams.processingUnit"
          placeholder="请输入加工单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="加工人" prop="processor">
        <el-input
          v-model="queryParams.processor"
          placeholder="请输入加工人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="成果影像质量检查人" prop="qualityInspector">
        <el-input
          v-model="queryParams.qualityInspector"
          placeholder="请输入成果影像质量检查人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文件大小(M)" prop="fileSize">
        <el-input
          v-model="queryParams.fileSize"
          placeholder="请输入文件大小(M)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="分辨率" prop="resolution">
        <el-input
          v-model="queryParams.resolution"
          placeholder="请输入分辨率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="拍摄单位" prop="shootingUnit">
        <el-input
          v-model="queryParams.shootingUnit"
          placeholder="请输入拍摄单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="拍摄地" prop="shootingLocation">
        <el-input
          v-model="queryParams.shootingLocation"
          placeholder="请输入拍摄地"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="拍摄日期" prop="shootingDate">
        <el-date-picker
          v-model="queryParams.shootingDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <!-- <el-form-item label="拍摄人" prop="photographer">
        <el-input
          v-model="queryParams.photographer"
          placeholder="请输入拍摄人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="拍摄设备品牌型号" prop="equipmentModel">
        <el-input
          v-model="queryParams.equipmentModel"
          placeholder="请输入拍摄设备品牌型号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="采集影像总数(张)" prop="totalImages">
        <el-input
          v-model="queryParams.totalImages"
          placeholder="请输入采集影像总数(张)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="采集影像质量检查人" prop="acquisitionInspector">
        <el-input
          v-model="queryParams.acquisitionInspector"
          placeholder="请输入采集影像质量检查人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- v-hasPermi="['dr:collection-image-acquisition:create']" -->
        <el-button v-if="!isView && !page" type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          v-if="!page"
          type="warning"
          plain
          @click="handleImport"
          v-hasPermi="['dr:collection-management:export']"
        >
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <!-- v-hasPermi="['dr:collection-image-acquisition:export']" -->
        <el-button v-if="!page" type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      height="calc(100vh - 320px)"
    >
      <el-table-column label="加工成果数据信息" align="center" prop="id">
        <!-- <el-table-column label="编号" align="center" prop="id" /> -->
        <!-- <el-table-column label="关联藏品ID" align="center" prop="collectionId" /> -->
        <el-table-column label="缩略图" align="center" prop="collectionCover">
          <template #default="scope">
            <el-image
              style="width: 44px; height: 44px"
              :src="scope.row.collectionCover"
              fit="cover"
              @click="imagePreview(scope.row.collectionCover)"
            />
          </template>
        </el-table-column>
        <el-table-column label="藏品总登记号" align="center" prop="generalAccessionNumber" />
        <el-table-column label="藏品名称" align="center" prop="collectionName" />
        <el-table-column label="影像文件名" align="center" prop="imageFilename" />
        <el-table-column label="文件格式" align="center" prop="fileFormat" />
        <el-table-column
          label="加工日期"
          align="center"
          prop="processingDate"
          :formatter="dateFormatter2"
        />
        <el-table-column label="加工单位" align="center" prop="processingUnit" />
        <el-table-column label="加工人" align="center" prop="processor" />
        <el-table-column label="成果影像质量检查人" align="center" prop="qualityInspector" />
        <el-table-column label="文件大小(M)" align="center" prop="fileSize" />
        <el-table-column label="分辨率" align="center" prop="resolution" />
      </el-table-column>
      <el-table-column label="采集数据信息" align="center" prop="id">
        <el-table-column label="拍摄单位" align="center" prop="shootingUnit" />
        <el-table-column label="拍摄地" align="center" prop="shootingLocation" />
        <el-table-column
          label="拍摄日期"
          align="center"
          prop="shootingDate"
          :formatter="dateFormatter2"
        />
        <el-table-column label="拍摄人" align="center" prop="photographer" />
        <el-table-column label="拍摄设备品牌型号" align="center" prop="equipmentModel" />
        <el-table-column label="采集内容" align="center" prop="acquisitionContent" />
        <el-table-column label="采集影像总数(张)" align="center" prop="totalImages" />
        <el-table-column label="采集影像质量检查人" align="center" prop="acquisitionInspector" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column label="审核状态" align="center" prop="sourceStatus" width="120px">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COLLECTION_AUDIT_STATUS" :value="scope.row.sourceStatus" />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="180px" fixed="right">
        <template #default="scope">
          <el-button
            v-if="!isView && !page"
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['dr:collection-image-acquisition:update']"
          >
            编辑
          </el-button>
          <el-button
            v-if="!isView && !page"
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['dr:collection-image-acquisition:delete']"
          >
            删除
          </el-button>
          <el-button
            v-if="['search', 'book'].includes(page)"
            link
            type="primary"
            @click="openForm('view', scope.row.id)"
            v-hasPermi="['dr:collection-image-acquisition:create']"
          >
            查看
          </el-button>
          <!-- 审批 -->
          <el-button
            v-if="!page && +scope.row.sourceStatus === 0"
            link
            type="primary"
            @click="handleAudit(scope.row.id)"
            v-hasPermi="['dr:collection-image-acquisition:update']"
          >
            审批
          </el-button>
          <el-button
            v-if="['download'].includes(page)"
            link
            type="primary"
            @click="openForm('download', scope.row.id)"
            v-hasPermi="['dr:collection-image-acquisition:create']"
          >
            下载
          </el-button>
          <el-button
            v-if="['share'].includes(page)"
            link
            type="primary"
            v-hasPermi="['dr:collection-image-acquisition:create']"
          >
            发布
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CollectionImageAcquisitionForm ref="formRef" :data="data" @success="getList" />
  <!-- 导入 -->
  <CollectionImportForm
    ref="importFormRef"
    :type="2"
    :collectionId="props.data.id"
    @success="getList"
  />
  <!-- 审批弹窗 -->
  <CollectionAuditDialog ref="auditDialogRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { createImageViewer } from '@/components/ImageViewer'
import {
  CollectionImageAcquisitionApi,
  CollectionImageAcquisitionVO
} from '@/api/dr/collectionimageacquisition'
import CollectionImageAcquisitionForm from './CollectionImageAcquisitionForm.vue'
import CollectionImportForm from '@/components/CollectionImportForm.vue'
import CollectionAuditDialog from './CollectionAuditDialog.vue'

/** 藏品二维采集信息 列表 */
defineOptions({ name: 'CollectionImageAcquisition' })
const props = defineProps({
  page: {
    type: String,
    default: '',
    validator: (val: string) => !val || ['download', 'search', 'book', 'share'].includes(val) // 下载、搜索、账
  },
  type: {
    type: String,
    default: '',
    validator: (val: string) => !val || ['add', 'edit', 'view'].includes(val)
  },
  data: {
    type: Object,
    default: () => ({})
  }
})
const isView = computed(() => props.type === 'view')

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CollectionImageAcquisitionVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  collectionId: undefined,
  imageFilename: undefined,
  fileFormat: undefined,
  generalAccessionNumber: undefined,
  processingDate: [],
  processingUnit: undefined,
  processor: undefined,
  qualityInspector: undefined,
  fileSize: undefined,
  resolution: undefined,
  shootingUnit: undefined,
  shootingLocation: undefined,
  shootingDate: [],
  photographer: undefined,
  equipmentModel: undefined,
  acquisitionContent: undefined,
  totalImages: undefined,
  acquisitionInspector: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CollectionImageAcquisitionApi.getCollectionImageAcquisitionPage({
      ...queryParams,
      collectionId: props.data.id
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CollectionImageAcquisitionApi.deleteCollectionImageAcquisition(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

// 查看图片
const imagePreview = (imgUrl: string) => {
  if (!imgUrl) {
    message.error('图片地址不存在')
    return
  }
  createImageViewer({
    zIndex: 9999999,
    urlList: [imgUrl]
  })
}

/** 导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CollectionImageAcquisitionApi.exportCollectionImageAcquisition(queryParams)
    download.excel(data, '藏品二维采集信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
// 审核操作
const auditDialogRef = ref()
const handleAudit = (id: number) => {
  auditDialogRef.value.open('audit', id)
}
watch(
  () => props.data.id,
  (id) => {
    if (id) {
      getList()
    }
  },
  { immediate: true }
)
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
